<?php

class ManageTicketHelpTopicController extends PsController {

	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'users' => array('@'),
						//'roles' => array('Transactions.ViewOwn'),
					)), parent::accessRules());
	}

	public function moduleAccessRules() {
		return array();
	}
	
	public function actionView($id) {
		$this->render('view', array(
			'ticketType' => $this->loadTicketType($id),
		));
	}

	public function actionCreate() {
		$ticketType = new TicketType;

		if (isset($_POST['TicketType'])) {
			$ticketType->setAttributes($_POST['TicketType']);
			$ticketType->agency_id = Yii::app()->user->model->agency_id;
			
			if ($ticketType->save()) {
				if (Yii::app()->getRequest()->getIsAjaxRequest()) {
					Yii::app()->end();
				}
			} else {	
				$this->redirect(array('view', 'id' => $ticketType->id));
			}
		}

		$this->render('create', array( 'ticketType' => $ticketType));
	}

	public function actionUpdate($id) {
		$ticketType = $this->loadTicketType($id);


		if (isset($_POST['TicketType'])) {
			$ticketType->setAttributes($_POST['TicketType']);

			if ($ticketType->save()) {
				$this->redirect(array('view', 'id' => $ticketType->id));
			}
		}

		$this->render('update', array(
				'ticketType' => $ticketType,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadTicketType($id)->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$ticketType = new TicketType('search');
		//$ticketType->unsetAttributes();
		$ticketType->agency_id = Yii::app()->user->model->agency_id;
		
		//dump($ticketType->search()); exit();
		//dump($ticketType->searchByAgency(Yii::app()->user->model->agency_id)); exit;
		
		if (isset($_GET['TicketType']))
			$ticketType->setAttributes($_GET['TicketType']);

		$this->render('index', array(
			'ticketType' => $ticketType,
		));
	}
	
	public function actionTest(){
		$dataa = array();
		$dataa[0] = count(TicketStatusHistory::model()->findAllByAttributes(array('ticket_status_type_id' => 1)));
		$dataa[1] = count(TicketStatusHistory::model()->findAllByAttributes(array('ticket_status_type_id' => 2)));
		$countticket = count (Ticket::model()->findAll());
		//dump($countticket);exit;
		$numberReopen = $dataa[0] - $countticket;
		$tmp = array(
			array(
				"value" => $countticket,
				"color" => "green",
				"label" => "Open"
			),
			array(
				"value" => $dataa[1],
				"color" => "red",
				"label" => "Close"
			),
			array(
				"value" => $numberReopen,
				"color" => "yellow",
				"label" => "Reopen"
			)
		);
		$this->render('test',array('tmp'=>$tmp));
	}
	
	public function actionAjaxupdate()
	{
		$act = $_GET['act'];
		//echo $act;exit;
		if($act=='doSortOrder')
		{
			$sortOrderAll = $_POST['priority'];
			if(count($sortOrderAll)>0)
			{
				foreach($sortOrderAll as $menuId=>$priority)
				{
					$model=$this->loadModel($menuId);
					$model->priority = $priority;
					$model->save();
				}
			}
		}
		else
		{
			$autoIdAll = $_POST['autoId'];
			if(count($autoIdAll)>0)
			{
				foreach($autoIdAll as $autoId)
				{
					$model=$this->loadTicketType($autoId);
					if($act=='doDelete'){
						
						if ($model->delete()){
							
						}
					}
					/*if($act=='doActive')
						$model->isActive = '1';
					if($act=='doInactive')
						$model->isActive = '0'; */    
					if($model->save())
						echo 'ok';
					else
						throw new PsPageNotFoundException();
	 
				}
			}
		}
	}
	
	/**
	 * Loads the TicketType AR model
	 * @param integer $id the primary key
	 * @return TicketType	 */
	public function loadTicketType($id) {
		//$ticketType = TicketType::model()->ofCurrentUser()->findByPk($id);
		$ticketType = TicketType::model()->findByPk($id);

		if ($ticketType == null) {
			throw new PsPageNotFoundException();
		}

		return $ticketType;
	}
}